package com.zhongyouyun.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {



    /*
        获得前N天日期
     */
    public static List<HashMap<Object, Object>> getDay(int day) {

        List<HashMap<Object, Object>> dateforList = new ArrayList<HashMap<Object, Object>>();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        String date = fmt.format(today);
        String maxDateStr = date;
        String minDateStr = "";
        Calendar calc = Calendar.getInstance();
        int k=0;

        try {
            for (int i = (day-1); i >= 0; i--) {
                k++;
                HashMap map = new HashMap();
                calc.setTime(fmt.parse(maxDateStr));
                calc.add(calc.DATE, -i);
                Date minDate = calc.getTime();
                minDateStr = fmt.format(minDate);
                map.put("date", minDateStr);
                map.put("day", k);
                map.put("value", 0);
                dateforList.add(map);

            }
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return dateforList;
    }



    /*
        获得本月日期
     */
    // month格式：yyyy-MM
    public static List<HashMap<Object, Object>> getMonthBetween(String month) {
        List<HashMap<Object, Object>> result = new ArrayList<>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(month + "-01"));

            int year = max.get(Calendar.YEAR);
            int mon = (max.get(Calendar.MONTH) + 1);
            String curMon = year + "-" + (mon < 10 ? "0" + mon : mon);
            if (month.equals(curMon)) {
                max.add(Calendar.DATE, -1);
            } else {
                max.setTime(sdf.parse(month + "-01"));
                max.add(Calendar.MONTH, 1);
            }
            while (min.before(max)) {
                HashMap param = new HashMap();
                param.put("date", sdf.format(min.getTime()));
                param.put("day", min.get(Calendar.DATE));
                result.add(param);
                min.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public  static List<HashMap<Object, Object>> getYearList(String year){

        List<HashMap<Object, Object>> list=new ArrayList<HashMap<Object, Object>>();
        for(int i=0;i<12;i++){
            HashMap map = new HashMap();
            String moun= String.valueOf(i+1);
            if (moun.length()==1){
                map.put("date", year+"-0"+(i+1));
                map.put("day", (i+1));
                map.put("value", 0);
                list.add(map);
            }else{
                map.put("date",year+"-"+(i+1));
                map.put("day", (i+1));
                map.put("value", 0);
                list.add(map);
            }

        }
        System.out.println(list);
        return  list;
    }
    public static void main(String[] args) {

        System.out.println(getYearList("2022"));

    }
}
